<template>
    <div class="app">
        <el-form label-position="right" label-width="80px" class="login-container" ref="ruleForm" :model="ruleForm"
            :rules="rules">
            <h3>新用户注册</h3>
            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="password2" required="true">
                <el-input type="password" v-model="ruleForm.password2"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="tel" required="true">
                <el-input v-model="ruleForm.tel"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button style="width: 220px;" type="primary" @click="submitForm('ruleForm')">注册</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    import md5 from 'js-md5'
    import { Register } from '@/api/index.js'
    export default {
        data() {
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            var validateTel = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入手机号'));
                } else if (!/1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}/.test(value)) {
                    callback(new Error('手机号格式不正确!'));
                } else {
                    callback();
                }
            };
            return {
                // 用户注册信息
                ruleForm: {
                    username: '',
                    password: '',
                    password2: '',// 确认密码
                    tel: '',
                },
               
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
                    ],
                    password2: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    tel: [
                        { validator: validateTel, trigger: 'blur' },
                    ],
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    // 规则校验通过
                    if (valid) {
                        delete this.ruleForm.password2
                        // 用户密码加密
                        this.ruleForm.password = md5(this.ruleForm.password + "lixiaoyu")
                        // 调用注册接口
                        Register(this.ruleForm).then((res) => {
                            // 注册成功
                            if (res.data.code === 200) {
                                this.$alert('即将跳转到登录页面！', '注册成功', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        this.$router.push('/login')
                                    }
                                });
                            } else {
                                // 注册失败
                                this.$message.error(res.data.msg);
                            }

                        })

                    } else {
                        // 规则校验失败
                        this.$message.error("输入内容不符合规则");
                        return false;
                    }
                });
            },
        }
    }
</script>
<style lang="less" scoped>
    .app {
        background-image: url('../assets/images/register_img.jpg');
        background-repeat: no-repeat;
        background-size: cover;
        background-position-y: center;
        position: fixed;
        height: 100%;
        width: 100%;
    }

    .login-container {
        width: 450px;
        border: 1px solid #eaeaea;
        margin: 0 auto;
        margin-top: 15vh;
        padding: 35px 35px 15px 35px;
        background-color: #fff;
        border-radius: 15px;
        box-sizing: border-box;

        h3 {
            text-align: center;
            margin-bottom: 40px;
        }

        .el-input {
            width: 280px;
        }

    }
</style>